package com.cskaoyan.controller;

import com.cskaoyan.bean.user.WxUserLoginData;
import com.cskaoyan.service.promotion.CouponService;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.promotion.WxCouponListVo;
import com.cskaoyan.vo.promotion.WxCouponMylistCouponVo;
import com.cskaoyan.vo.promotion.WxCouponMylistVo;
import com.cskaoyan.bo.promotion.WxCouponReceiveBo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @description:
 * @autyor:caicaicai
 */
@RestController
public class WxCouponController {

    @Autowired
    CouponService couponService;

    @RequestMapping("/wx/coupon/list")//?page=1&size=10
    public BaseRespVo list(Integer page, Integer size){
        WxCouponListVo wxCouponListVo = couponService.queryWx(page, size);
        return BaseRespVo.ok(wxCouponListVo);
    }

    @RequestMapping("/wx/coupon/mylist")//?status=0&page=1&size=10
    public BaseRespVo mylist(Integer size, Integer page, Short status){
        Subject subject = SecurityUtils.getSubject();
        WxUserLoginData loginData = (WxUserLoginData) subject.getPrincipal();
        Integer userId = loginData.getId();
        // Integer userId = 2;
        WxCouponMylistVo wxCouponMylistVo = couponService.queryMylistWx(userId, page, size, status);
        return BaseRespVo.ok(wxCouponMylistVo);
    }

    @RequestMapping("/wx/coupon/selectlist")//?cartId=26&grouponRulesId=0
    public BaseRespVo selectlist(Integer cartId, Integer grouponRulesId){
        Subject subject = SecurityUtils.getSubject();
        WxUserLoginData loginData = (WxUserLoginData) subject.getPrincipal();
        Integer userId = loginData.getId();
        // Integer userId = 2;
        List<WxCouponMylistCouponVo> couponVos = couponService.selectListWx(userId, cartId, grouponRulesId);
        return BaseRespVo.ok(couponVos);
    }

    @RequestMapping("/wx/coupon/receive")//{couponId: 45}
    public BaseRespVo receive(@RequestBody WxCouponReceiveBo receiveBo){
        // Integer userId = 2;
        Subject subject = SecurityUtils.getSubject();
        WxUserLoginData loginData = (WxUserLoginData) subject.getPrincipal();
        Integer userId = loginData.getId();
        int code = couponService.receiveWx(userId, receiveBo);
        //根据不同情况响应不同结果
        if(code == 740){
            return BaseRespVo.error(740, "优惠券已领完");
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("/wx/coupon/exchange")
    public BaseRespVo exchange(@RequestBody Map<String, String> codeMap){
        Subject subject = SecurityUtils.getSubject();
        WxUserLoginData loginData = (WxUserLoginData) subject.getPrincipal();
        Integer userId = loginData.getId();
        // Integer userId = 2;
        String code = codeMap.get("code");
        int codeVo = couponService.exchange(userId, code);
        if(codeVo == 740){
            return BaseRespVo.error(740, "优惠券已兑换");
        }else if(codeVo == 742){
            return BaseRespVo.error(742, "优惠券不正确");
        }
        return BaseRespVo.ok();
    }

}
